import { useUserStore } from '@/stores/user'

/**
 * 权限指令
 * 使用方法：v-permission="'system:user:add'"
 */
export const permission = {
  mounted(el, binding) {
    const { value } = binding
    const userStore = useUserStore()
    
    if (value) {
      const hasPermission = userStore.hasPermission(value)
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('需要指定权限标识，如 v-permission="\'system:user:add\'"')
    }
  }
}

/**
 * 角色指令
 * 使用方法：v-role="'admin'"
 */
export const role = {
  mounted(el, binding) {
    const { value } = binding
    const userStore = useUserStore()
    
    if (value) {
      const hasRole = userStore.hasRole(value)
      if (!hasRole) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('需要指定角色标识，如 v-role="\'admin\'"')
    }
  }
}